package com.example.w.web.controller;

import com.example.w.common.dto.MenuItemAddDTO;
import com.example.w.common.dto.MenuItemUpdateDTO;
import com.example.w.common.exception.SystemException;
import com.example.w.common.pojo.MenuItem;
import com.example.w.common.pojo.TypeNum;
import com.example.w.common.util.Result;
import com.example.w.service.MenuItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menuItem")
@Api(tags = "菜品管理接口")
public class MenuItemController {
    @Autowired
    private MenuItemService menuItemService;

    @PostMapping("/addMenuItem")
    @ApiOperation("菜品添加")
    //@RequiresRoles(value = {"menuItemManager", "admin"}, logical = Logical.OR)
    public Result addMenuItem(@RequestBody MenuItemAddDTO menuItemAddDTO) {
        MenuItem target = new MenuItem();
        BeanUtils.copyProperties(menuItemAddDTO, target);
        //检查输入的菜品类型
        if(!ObjectUtils.isEmpty(menuItemAddDTO.checkTypeCorrect())){
            target.setType(menuItemAddDTO.checkTypeCorrect());
        }
        Long id = menuItemService.addMenuItem(target, menuItemAddDTO.getSingleItemIds());
        return new Result(true, "添加成功", id);
    }

    @PostMapping("/updateMenuItem")
    @ApiOperation("菜品信息更新")
    //@RequiresRoles(value = {"menuItemManager", "admin"}, logical = Logical.OR)
    public Result updateMenuItem(@RequestBody MenuItemUpdateDTO menuItemUpdateDTO) {
        //检查更新菜品的信息是否完整
        if (!menuItemUpdateDTO.checkPriceCorrect()){
            throw new SystemException("更新失败，价格信息不符合要求");
        }
        MenuItem target = new MenuItem();
        BeanUtils.copyProperties(menuItemUpdateDTO, target);
        menuItemService.updateMenuItem(target);
        return new Result(true, "更新成功");
    }

    @GetMapping("/removeMenuItem")
    @ApiOperation("删除菜品信息")
    //@RequiresRoles(value = {"menuItemManager", "admin"}, logical = Logical.OR)
    public Result removeMenuItem(@RequestParam Long id) {
        menuItemService.removeMenuItem(id);
        return new Result(true, "删除成功");
    }

    @PostMapping("/listAllMenuItem")
    @ApiOperation("查出所有商品信息")
    //@RequiresRoles(value = {"menuItemManager", "admin"}, logical = Logical.OR)
    public Result listAllMenuItem() {
        List<MenuItem> menuItemList = menuItemService.listAllMenuItem();
        return new Result(true, "查询成功", menuItemList);
    }

    @GetMapping("/getMenuItemById")
    @ApiOperation("查找指定菜品信息")
    //@RequiresRoles(value = {"menuItemManager", "admin"}, logical = Logical.OR)
    public Result getMenuItemById(@RequestParam Long id) {
        MenuItem target = menuItemService.getMenuItemById(id);
        return new Result(true, "查询成功", target);
    }

    @GetMapping("/getMenuItemByType")
    @ApiOperation("根据菜品分类查找菜品")
    //@RequiresRoles(value = {"menuItemManager", "admin"}, logical = Logical.OR)
    public Result getMenuItemsByType(TypeNum type) {
        List<MenuItem> target = menuItemService.getMenuItemsByType(type);
        return new Result(true, "查询成功", target);
    }
}
